import { Component, OnInit } from "@angular/core";
import { CesiumMap } from "src/app/cesium-map/utils/cesiumMap";
import { InitCesiumService } from "src/app/cesium-map/services/InitCesiumService";
declare var Cesium;
@Component({
  selector: 'app-case2',
  templateUrl: './case2.component.html',
  styleUrls: ['./case2.component.less']
})
export class Case2Component implements OnInit {
  options:any={
    shadows:false
  }
  cesiumMap: CesiumMap;
  Viewer: any;
  params;
  lanTianOSGB;
  constructor(private InitCesiumService: InitCesiumService) {}

  ngOnInit() {
    this.cesiumMap = new CesiumMap();
   
  }

  mapCreate($event) {
    this.Viewer = $event;
    this.Viewer.clock.currentTime = new Cesium.JulianDate(2457522.154792);
    // 检查器相关操作
    this.Viewer.extend(Cesium.viewerCesium3DTilesInspectorMixin);
    var inspectorViewModel =  this.Viewer.cesium3DTilesInspector.viewModel;
    var scene = this.Viewer.scene;
    var tileset;
    var viewModel = {
      tilesets: [
          {
              name: '汕头数据',
              resource: '/files/dixingtu/fengkai-tileset/tileset.json'
          },
          {
              name: 'Tileset',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Tilesets/Tileset/tileset.json'
          }, {
              name: 'Translucent',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Batched/BatchedTranslucent/tileset.json'
          }, {
              name: 'Translucent/Opaque',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Batched/BatchedTranslucentOpaqueMix/tileset.json'
          }, {
              name: 'Multi-color',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Batched/BatchedColors/tileset.json'
          }, {
              name: 'Request Volume',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Tilesets/TilesetWithViewerRequestVolume/tileset.json'
          }, {
              name: 'Batched',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Batched/BatchedWithBatchTable/tileset.json'
          }, {
              name: 'Instanced',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Instanced/InstancedWithBatchTable/tileset.json'
          }, {
              name: 'Instanced/Orientation',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Instanced/InstancedOrientation/tileset.json'
          }, {
              name: 'Composite',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/Composite/Composite/tileset.json'
          }, {
              name: 'PointCloud',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/PointCloud/PointCloudRGB/tileset.json'
          }, {
              name: 'PointCloudConstantColor',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/PointCloud/PointCloudConstantColor/tileset.json'
          }, {
              name: 'PointCloudNormals',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/PointCloud/PointCloudNormals/tileset.json'
          }, {
              name: 'PointCloudBatched',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/PointCloud/PointCloudBatched/tileset.json'
          }, {
              name: 'PointCloudDraco',
              resource: 'http://www.zgeo.work/cesiumTx/data/Cesium3DTiles/PointCloud/PointCloudDraco/tileset.json'
          }
      ],
      selectedTileset: undefined,
      shadows: true
     };

     Cesium.knockout.track(viewModel);

    // 图层选择器
    var toolbar = document.getElementById('toolbar');
    Cesium.knockout.applyBindings(viewModel, toolbar);
    let _this=this
    Cesium.knockout.getObservable(viewModel, 'shadows').subscribe(function(enabled) {
      _this.Viewer.shadows = enabled;
    });

    // 检查器相关操作
    Cesium.knockout.getObservable(viewModel, 'selectedTileset').subscribe(function(options) {
        if (Cesium.defined(tileset)) {
            scene.primitives.remove(tileset);
        }
        if (!Cesium.defined(options)) {
            inspectorViewModel.tileset = undefined;
            return;
        }

        tileset =  scene.primitives.add(new Cesium.Cesium3DTileset({
            url : options.resource
        }));

        tileset.readyPromise.then(function() {
            inspectorViewModel.tileset = tileset;
            _this.Viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(0, -2.0, Math.max(100.0 - tileset.boundingSphere.radius, 0.0)));
            var properties = tileset.properties;
            if (Cesium.defined(properties) && Cesium.defined(properties.Height)) {
                tileset.style = new Cesium.Cesium3DTileStyle({
                    color: {
                        conditions: [
                            ['${Height} >= 83', "color('purple', 0.5)"],
                            ['${Height} >= 80', "color('red')"],
                            ['${Height} >= 70', "color('orange')"],
                            ['${Height} >= 12', "color('yellow')"],
                            ['${Height} >= 7', "color('lime')"],
                            ['${Height} >= 1', "color('cyan')"],
                            ['true', "color('blue')"]
                        ]
                    }
                });
            }
        }).catch(function(error) {
            throw(error);
        });
    });

    viewModel.selectedTileset = viewModel.tilesets[0];

    var handler = new Cesium.ScreenSpaceEventHandler( this.Viewer.canvas);

    handler.setInputAction(function(movement) {
        var feature = inspectorViewModel.feature;
        if (Cesium.defined(feature)) {
            var propertyNames = feature.getPropertyNames();
            var length = propertyNames.length;
            for (var i = 0; i < length; ++i) {
                var propertyName = propertyNames[i];
                console.log(propertyName + ': ' + feature.getProperty(propertyName));
            }
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    // handler.setInputAction(function(movement) {
    //     var feature = inspectorViewModel.feature;
    //     if (Cesium.defined(feature)) {
    //         feature.show = false;
    //     }
    // }, Cesium.ScreenSpaceEventType.MIDDLE_CLICK);
    this.gohome();
  }

 
  /**
   * 回到视野
   */
  gohome() {
    this.Viewer.camera.flyTo({
      // fromDegrees()方法，将经纬度和高程转换为世界坐标
      destination: Cesium.Cartesian3.fromDegrees(
        111.60829020123631,
        23.453477687796436,
        1000
      ),
      orientation: {
        heading: 6, //代表镜头左右方向,正值为右,负值为左,360度和0度是一样的
        // 视角
        pitch: -0.3187528970141864, //俯视角，默认-90，垂直向下
        roll: 0, // 旋转角
      },
      duration: 5,
    });
  }
}
